<?php
class ArticleModel
{
	public static function Add($title, $alias, $categoryID, 
	$state, $access, $introContent, $content, $createdBy,
	$createdDate, $startPublishing, $finishPublishing,
	$metaDes, $metaKeyword)
	{
		include_once 'sql_connector.php';
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		
		$data = array('id'=>'', 'title'=>$title, 'alias'=>$alias,
		'CategoryID'=>$categoryID, 'State'=>$state, 'Access'=>$access, 
		'IntroContent'=>$introContent, 'Content'=>$content, 
		'CreatedByID'=>$createdBy,
		'CreatedTime'=>$createdDate, 'PublicUp'=>$startPublishing, 'PublicDown'=>$finishPublishing,
		'MetaDes'=>$metaDes, 'MetaKeyword'=>$metaKeyword);
		try {
			$result = $db->insert('articles', $data);
		}
		catch(Zend_Db_Exception $e)
		{
			echo 'DB ERROR : '.$e->getMessage();
		}
		
		return $result;
	}
	
	public static function GetArticleByCategoryID($categoryID)
	{
		include_once 'sql_connector.php';
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		
		if($categoryID != -1){
			$query = "select * from articles where categoryid = '$categoryID'";
		}
		else {
			$query = "select * from articles";
		}
		try {
			$result = $db->fetchAll($query);
		}
		catch(Zend_Db_Exception $e){
			echo 'DB ERROR : '.$e->getMessage();
		}
		return $result;
	}
	
public static function GetArticleByID($ID)
	{
		include_once 'sql_connector.php';
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		
		$query = "select * from articles where ID = '$ID'";
		try {
			$result = $db->fetchRow($query);
		}
		catch(Zend_Db_Exception $e){
			echo 'DB ERROR : '.$e->getMessage();
		}
		return $result;
	}
	
	public static function UpdateState($IDs, $value)
	{
		include_once 'sql_connector.php';
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		
		try {
			foreach ($IDs as $ID){
				$where = "ID = '$ID'";
				$set = array('state' => $value);
				$db->update('articles', $set, $where);
			}
		}
		catch(Zend_Db_Exception $e){
			echo 'DB ERROR : '.$e->getMessage();
		}
	}
	
	public static function Delete($IDs)
	{
		include_once 'sql_connector.php';
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		
		try {
			foreach ($IDs as $ID){
				$db->delete('articles', "ID = '$ID'");
			}
		}
		catch(Zend_Db_Exception $e){
			echo 'DB ERROR : '.$e->getMessage();
		}
	}
	
	public static function Update($ID, $title, $alias, $categoryID, 
	$state, $access, $introContent, $content, $createdBy,
	$createdDate, $startPublishing, $finishPublishing,
	$metaDes, $metaKeyword)
	{
		include_once 'sql_connector.php';
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		
		$where = array("ID = '$ID'");
		$data = array('title'=>$title, 'alias'=>$alias,
		'CategoryID'=>$categoryID, 'State'=>$state, 'Access'=>$access, 
		'IntroContent'=>$introContent, 'Content'=>$content, 
		'CreatedByID'=>$createdBy,
		'CreatedTime'=>$createdDate, 'PublicUp'=>$startPublishing, 'PublicDown'=>$finishPublishing,
		'MetaDes'=>$metaDes, 'MetaKeyword'=>$metaKeyword);
		try {
			$result = $db->update('articles', $data, $where);
		}
		catch(Zend_Db_Exception $e)
		{
			echo 'DB ERROR : '.$e->getMessage();
		}
		
		return $result;
	}
}